package com.hou.config;


import com.hou.pojo.Vip;
import com.hou.service.IVipService;
import com.hou.vo.CaseVip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
public class HandlerArgumentResolver implements HandlerMethodArgumentResolver {
    @Autowired
    IVipService iVipService;

    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        if (parameter.getParameterType() == Vip.class// 参数类型为UsWerBean
                && parameter.hasParameterAnnotation(CaseVip.class)// 参数标注注解为CaseUser
        ) {
            return true;
        }
        return false;
    }


    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest Request = webRequest.getNativeRequest(HttpServletRequest.class);
        HttpServletResponse Response = webRequest.getNativeResponse(HttpServletResponse.class);
//        String s = CookiesUtil.readLoginToken(Request);
        String token = Request.getHeader("Authorization");
        if (token == null) {
            return null;
        }
//        UserBasic user = iUserBasicService.getUserByCookies(s, Response, Request);
        Vip vip = (Vip) redisTemplate.opsForValue().get("VIP:" + token);

        return vip;
    }
}
